#include "game.h"

static void load_images(s_object *object)
{
    object->img[0] = IMG_Load("../check/img/nvlherbe.png");
    object->img[1] = IMG_Load("../check/img/terre.png");
    object->img[2] = IMG_Load("../check/img/rocher.png");
    object->img[3] = IMG_Load("../check/img/arbre.png");
    object->img[4] = IMG_Load("../check/img/start.png");
    object->img[5] = IMG_Load("../check/img/exit.png");
    object->side = IMG_Load("../check/img/right_side.png");

    object->tow[0] = IMG_Load("../check/img/tow_lambda.png");
    object->tow[1] = IMG_Load("../check/img/tow_advanced.png");
    object->tow[2] = IMG_Load("../check/img/tow_power.png");
    object->enn[0] = IMG_Load("../check/img/enn_lambda.png");
}

static void unload_images(s_object *object)
{
    for (int i = 0; i < TAB_SIZE; i++)
        SDL_FreeSurface(object->img[i]);
    for (int i = 0; i < ENN_SIZE; i++)
        SDL_FreeSurface(object->enn[i]);
    SDL_FreeSurface(object->side);
}

static void parse_screen(s_object *object, SDL_Rect *pos, int i, int j)
{
    pos->y = i * IMG_Y;
    pos->x = j * IMG_X;
    if (object->mat[i][j] == B_ROAD_GROUND || object->mat[i][j] == 'X')
        SDL_BlitSurface(object->img[1], NULL, object->screen, pos);
    else if (object->mat[i][j] == B_ROCK || object->mat[i][j] == 'R')
    {
        SDL_BlitSurface(object->img[0], NULL, object->screen, pos);
        SDL_BlitSurface(object->img[2], NULL, object->screen, pos);
    }
    else if (object->mat[i][j] == B_TREE || object->mat[i][j] == 'A')
    {
        SDL_BlitSurface(object->img[0], NULL, object->screen, pos);
        SDL_BlitSurface(object->img[3], NULL, object->screen, pos);
    }
    else if (object->mat[i][j] == B_ENTRY)
        SDL_BlitSurface(object->img[4], NULL, object->screen, pos);
    else if (object->mat[i][j] == B_EXIT)
        SDL_BlitSurface(object->img[5], NULL, object->screen, pos);
    else
        SDL_BlitSurface(object->img[0], NULL, object->screen, pos);
}
/**
** @brief Prints the matrix
**
** @param Matrix of the map
*/
static void print_screen(s_object *object)
{
    SDL_Rect pos;
    int i;
    int j;

    for (i = 0; i < MAT_H; i++)
    {
        for (j = 0; j < MAT_W; j++)
            parse_screen(object, &pos, i, j);
    }
    pos.x = 24 * 40;
    pos.y = 0;
    SDL_BlitSurface(object->side, NULL, object->screen, &pos);
    pos.x = 24 * 40 + 60;
    pos.y = 200;
    SDL_BlitSurface(object->tow[object->tow_type], NULL, object->screen, &pos);
}

/**
** @brief Main Loop of the game
*/
static int update(s_object *object)
{
    char        cond = 1;
    int         time[3] = {0, 0, 0};
    int         delay = 0;
    SDL_Event   event;

    while (cond == 1)
    {
        SDL_PollEvent(&event);
        switch (event.type)
        {
            case SDL_QUIT:
                cond = 0;
                break;
            default:
                mouse_click(object, event);
                cond = (object->life > 0);
                break;
        }
        if ((time[0] = SDL_GetTicks()) && time[0] - time[1] > delay)
            if ((time[1] = time[0]) && !launch_new_enn(object, &delay))
                return (-1);
        update_wave(object, time);
    }
    return (cond);
}

void draw (s_object *object)
{
    char string[11];
    SDL_Rect position = { .x = 24 * 40 + 30, .y =  10};
    SDL_Surface *text;

    print_screen(object);
    sprintf(string, "Life: %d", object->life);
    text = texte_surface(string);
    SDL_BlitSurface(text, NULL, object->screen, &position);
    close_print(text);

    sprintf(string, "Gold: %d", object->gold);
    text = texte_surface(string);
    position.y += 40;
    SDL_BlitSurface(text, NULL, object->screen, &position);
    close_print(text);

    sprintf(string, "Wave: %d", object->wave);
    text = texte_surface(string);
    position.y += 40;
    SDL_BlitSurface(text, NULL, object->screen, &position);
    close_print(text);

    print_tow_list(object);
    print_enn_list(object);

    SDL_Flip(object->screen);
}

/**
** @brief Create a SDL window
**
** @return Surface i.e the window
*/
static SDL_Surface *create_window()
{
    SDL_Surface *screen = NULL;

    screen = SDL_SetVideoMode(40 * 24 + 160, 960, 32, SDL_HWSURFACE |
            SDL_RESIZABLE | SDL_DOUBLEBUF);
    SDL_WM_SetCaption("TheTDoftheroxxors", NULL);

    return (screen);
}

static s_object *init(void)
{
    s_object *object = NULL;

    if ((object = malloc(sizeof (s_object))))
    {
        object->tow_list = NULL;
        object->enn_list = NULL;
        object->fifo = NULL;
        object->life = 25;
        object->gold = 100;
        object->wave = 0;
        object->tow_type = E_LAMBDA;
    }
    return (object);
}

int main(void)
{
    s_object *object;

    object = init();

    get_mat("../check/map1", object);
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) == -1)
    {
        fprintf(stderr, "SDL Error :%s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }

    object->screen = create_window();
    if (!start_up(object))
    {
        load_images(object);
        print_screen(object);

        if (!update(object))
            screen_fail(object);
        else
            screen_win(object);
        unload_images(object);
    }
    SDL_Quit();

    return (EXIT_SUCCESS);
}
